#include "kitmainwindow.h"
#include "ui_kitmainwindow.h"
#include <QString>
#include <QPainter>
#include <QImage>
#include <QBoxLayout>
#include "qmessagebox.h"
#include "bezier_label.h"

float map_width = 0;
float map_height = 0;
float map_width_pixel = 0;
float map_height_pixel = 0;

Bezier *bezier_path = new Bezier[10];

int bezier_num = 1;
int bezier_cnt = 0;
double translate_dx = 0;
double translate_dy = 0;
double translate_dangle = 0;

Bezier_Label *bezier_label;
/***************************************************************
  *  @brief     主窗口初始化
  *  @param     parent,继承了QWidget类
  *  @note      无
  *  @Sample usage     无
  *  @author    zh
  *  @      2020/2/17
 **************************************************************/
KitMainWindow::KitMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::KitMainWindow)
{
    ui->setupUi(this);
    img = new QImage;
    img->load(":/Path_image/ground.png");

    bezier_label = new Bezier_Label(ui->imglabel);
    bezier_label->setPixmap( QPixmap::fromImage(*img) );
    bezier_label->resize( img->size() );
    bezier_label->setGeometry(0,0,img->size().width(),img->size().height());

    map_width_pixel = img->width();
    map_height_pixel = img->height();
    map_width = ui->Edit_map_width->text().toFloat();
    map_height = ui->Edit_map_heigh->text().toFloat();
}

KitMainWindow::~KitMainWindow()
{
    delete ui;
}

void KitMainWindow::on_Button_bezier_num_clicked()
{
    if( ui->Edit_bezier_num->text().isEmpty() )
    {
        QMessageBox::warning(NULL, "贝塞尔曲线数量不能为0", "请输入正确的数目", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }
    bezier_num = ui->Edit_bezier_num->text().toInt();
}

void KitMainWindow::on_Button_load_path_clicked()
{
    translate_dx = ui->Edit_translate_dx->text().toDouble();
    translate_dy = ui->Edit_translate_dy->text().toDouble();
    translate_dangle = ui->Edit_translate_dangle->text().toDouble(); 
    bezier_label->update();
}


void KitMainWindow::on_Button_add_bezier_clicked()
{
    bezier_cnt = (bezier_cnt + 1) % bezier_num;
    ui->Edit_bezier_cnt->clear();
    QString cnt = "";
    cnt.sprintf("%d",bezier_cnt);
    ui->Edit_bezier_cnt->setText(cnt);
}

void KitMainWindow::on_Button_create_path_clicked()
{
    if( ui->Edit_bezier_num->text().isEmpty() )
    {
        QMessageBox::warning(NULL, "贝塞尔曲线数量不能为0", "请输入正确的数目", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }
    bezier_path[bezier_cnt].input_num = 0;
    bezier_path[bezier_cnt].num_btw_two = ui->Edit_num_btw_two->text().toInt();
    bezier_path[bezier_cnt].k_speed = ui->Slider_kspeed->value();
    bezier_path[bezier_cnt].max_speed = ui->Edit_max_speed->text().toInt();
    bezier_path[bezier_cnt].now_angle = ui->Edit_now_angle->text().toFloat();
    bezier_path[bezier_cnt].target_angle = ui->Edit_target_angle->text().toFloat();

    if( !ui->Edit_x0->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[0].X = ui->Edit_x0->text().toFloat();
        bezier_path[bezier_cnt].input_points[0].Y = ui->Edit_y0->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    if( !ui->Edit_x1->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[1].X = ui->Edit_x1->text().toFloat();
        bezier_path[bezier_cnt].input_points[1].Y = ui->Edit_y1->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    if( !ui->Edit_x2->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[2].X = ui->Edit_x2->text().toFloat();
        bezier_path[bezier_cnt].input_points[2].Y = ui->Edit_y2->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    if( !ui->Edit_x3->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[3].X = ui->Edit_x3->text().toFloat();
        bezier_path[bezier_cnt].input_points[3].Y = ui->Edit_y3->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    if( !ui->Edit_x4->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[4].X = ui->Edit_x4->text().toFloat();
        bezier_path[bezier_cnt].input_points[4].Y = ui->Edit_y4->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    if( !ui->Edit_x5->text().isEmpty() )
    {
        bezier_path[bezier_cnt].input_points[5].X = ui->Edit_x5->text().toFloat();
        bezier_path[bezier_cnt].input_points[5].Y = ui->Edit_y5->text().toFloat();
        bezier_path[bezier_cnt].input_num++;
    }

    bezier_path[bezier_cnt].out_num = (bezier_path[bezier_cnt].input_num - 1) * bezier_path[bezier_cnt].num_btw_two;

    bezier_path[bezier_cnt].ctrlpoints_get();

    bezier_path[bezier_cnt].outpoints_get();

    bezier_path[bezier_cnt].calculate_length();

    bezier_path[bezier_cnt].direct_planning();

    bezier_path[bezier_cnt].angle_planning();

    if(bezier_path[bezier_cnt].input_num == 2)
    {
        bezier_path[bezier_cnt].strghtline_speed_plan();
    }
    else
    {
        bezier_path[bezier_cnt].speed_planning();
    }

    /*输入点提醒*/
    ui->plainTextEdit_input->clear();
    QString input_warning = "";
    input_warning.sprintf("第%d条贝塞尔曲线\r\n",bezier_cnt);
    ui->plainTextEdit_input->insertPlainText(input_warning);
    for(int i = 0; i < bezier_path[bezier_cnt].input_num; i++)
    {
        input_warning.sprintf("第%d个点,(%.3f,%.3f)\r\n",i,bezier_path[bezier_cnt].input_points[i].X,bezier_path[bezier_cnt].input_points[i].Y);
        ui->plainTextEdit_input->insertPlainText(input_warning);
    }

    /*输出点个数*/
    ui->Edit_point_num->clear();
    QString bezier_num = "";
    bezier_num.sprintf("%d",bezier_path[bezier_cnt].out_num+1);
    ui->Edit_point_num->setText(bezier_num);

    /*路径长度*/
    ui->Edit_bezier_length->clear();
    QString bezier_length = "";
    bezier_length.sprintf("%.3f",bezier_path[bezier_cnt].length);
    ui->Edit_bezier_length->setText(bezier_length);

    /*输出点提醒*/
    ui->plainTextEdit_out->clear();
    QString out_warning = "";
    out_warning.sprintf("第%d条贝塞尔曲线\r\n",bezier_cnt);
    ui->plainTextEdit_out->insertPlainText(out_warning);
    for (int i = 0; i <= bezier_path[bezier_cnt].out_num; i++)
    {
        out_warning.sprintf("第%d个点: X=%.3f   Y=%.3f   speed=%d   direct=%.3f   angle=%.3f\r\n",
                            i, bezier_path[bezier_cnt].out_points[i].X, bezier_path[bezier_cnt].out_points[i].Y,
                            bezier_path[bezier_cnt].speed[i], bezier_path[bezier_cnt].direct[i], bezier_path[bezier_cnt].angle[i]);
        ui->plainTextEdit_out->insertPlainText(out_warning);
    }
}

void KitMainWindow::on_Button_create_file_clicked()
{
    if( ui->Edit_file_location->text().isEmpty() )
    {
        QMessageBox::warning(NULL, "无路径", "请输入生成文件路径", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }

    QString address = ui->Edit_file_location->text();
    QFile file(address);;
    if(file.open(QFile::WriteOnly|QFile::Truncate| QIODevice::Text | QIODevice::Append))
    {
        char*  ch;
        QString code;
        QByteArray ba = "#include<point.h>\r\n";
        ba.append("/*X    Y   SPEED   DIRECT  ANGLE*/ \r\n");

        for (int i = 0; i < bezier_num; i++)
        {
            code.sprintf("/*第%d条贝塞尔曲线，共有%d个点*/",i,bezier_path[i].out_num + 1);
            code.append("\r\n");
            ba.append(code);
            code.sprintf("Points points_pos[%d]{\r\n",i);
            ba.append(code);
            for(int j = 0; j <= bezier_path[i].out_num; j++)
            {
                code.sprintf("{%f,     %f,     %d,     %f,     %f},",
                             bezier_path[i].out_points[j].X, bezier_path[i].out_points[j].Y,
                             bezier_path[i].speed[j],bezier_path[i].direct[j],bezier_path[i].angle[j]);
                ba.append(code);
                code.sprintf("      /*第%d个点*/",j);
                code.append("\r\n");
                ba.append(code);
            }
            code.sprintf("};\r\n");
            ba.append(code);
        }

        ch=ba.data();
        file.write(ch);
        file.close();
    }

    QMessageBox::information(NULL, "提示", "保存点成功", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
}

void KitMainWindow::on_Button_clear_clicked()
{
    ui->plainTextEdit_out->clear();
    ui->plainTextEdit_input->clear();
    ui->Edit_bezier_cnt->setText("0");
    if(bezier_path)
    {
        delete bezier_path;
        Bezier *bezier_path = new Bezier[10];
    }
    bezier_cnt = 0;
    bezier_num = 1;
}
